package com.example.demo.controller;

import com.example.demo.model.*;
import com.example.demo.service.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/query")
public class QueryController {

    @Autowired
    private QueryService queryService;

    @RequestMapping(value = "/getAllProvinces", method = RequestMethod.GET)
    @ResponseBody
    private RESTfulResult<List<ProvinceEntity>> getAllProvinces() {
        var result = new RESTfulResult<List<ProvinceEntity>>();
        var data = this.queryService.getAllProvinces();
        result.setData(data);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "/getCitiesByProvinceAdcode", method = RequestMethod.GET)
    @ResponseBody
    private RESTfulResult<List<CityEntity>> getCitiesByProvinceAdcode(@RequestParam("adcode") String adcode) {
        var result = new RESTfulResult<List<CityEntity>>();
        var data = this.queryService.getCitiesByProvinceAdcode(adcode);
        result.setData(data);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "/getDistrictsByCityAdcode", method = RequestMethod.GET)
    @ResponseBody
    private RESTfulResult<List<DistrictEntity>> getDistrictsByCityAdcode(@RequestParam("adcode") String adcode) {
        var result = new RESTfulResult<List<DistrictEntity>>();
        var data = this.queryService.getDistrictsByCityAdcode(adcode);
        result.setData(data);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }

    @RequestMapping(value = "/getAreaByAdcode", method = RequestMethod.GET)
    @ResponseBody
    private RESTfulResult<AreaEntity> getAreaByAdcode(@RequestParam("adcode") String adcode) {
        var result = new RESTfulResult<AreaEntity>();
        var data = this.queryService.getAreaByAdcode(adcode);
        result.setData(data);
        result.setCode(200);
        result.setSuccess(true);
        return result;
    }

}
